package com.tiecode.plugin.regex.editor.decision;

import java.util.Arrays;
import java.util.Collections;

import io.github.scave.lsp4a.highlight.decision.Decision;
import io.github.scave.lsp4a.highlight.decision.DecisionResult;
import io.github.scave.lsp4a.highlight.token.IdentifierToken;
import io.github.scave.lsp4a.highlight.token.TokenModel;

public abstract class AbstractDecision implements Decision<State> {
    public TokenModel getNextNonEmptyToken(TokenModel model) {
        if (model == null) {
            return null;
        }
        model = model.next;
        while (model != null) {
            if (model.token != IdentifierToken.WHITESPACE) {
                return model;
            }
            model = model.next;
        }
        return null;
    }

    public TokenModel getPrevNonEmptyToken(TokenModel model) {
        if (model == null) {
            return null;
        }
        model = model.prev;
        while (model != null) {
            if (model.token != IdentifierToken.WHITESPACE) {
                return model;
            }
            model = model.prev;
        }
        return null;
    }

    public Iterable<DecisionResult> single(DecisionResult result) {
        return Collections.singleton(result);
    }

    public Iterable<DecisionResult> singleColor(int schema) {
        DecisionResult result = new DecisionResult();
        result.kind = DecisionResult.ResultKind.ACCEPT;
        result.content = new DecisionResult.StyleContent(schema);
        return Collections.singleton(result);
    }

    public Iterable<DecisionResult> singleColorBold(int schema) {
        DecisionResult result = new DecisionResult();
        result.kind = DecisionResult.ResultKind.ACCEPT;
        result.content = new DecisionResult.StyleContent(schema, true, false);
        return Collections.singleton(result);
    }

    public Iterable<DecisionResult> singleColorItalic(int schema) {
        DecisionResult result = new DecisionResult();
        result.kind = DecisionResult.ResultKind.ACCEPT;
        result.content = new DecisionResult.StyleContent(schema, false, true);
        return Collections.singleton(result);
    }

    public Iterable<DecisionResult> results(DecisionResult... result) {
        return Arrays.asList(result);
    }

    public Iterable<DecisionResult> refuse() {
        DecisionResult result = new DecisionResult();
        result.kind = DecisionResult.ResultKind.REFUSE;
        return single(result);
    }
}
